from typing import List
from datetime import datetime
from dataclasses import dataclass, asdict, fields

@dataclass(init=False)
class Details:
    parent_model: str
    format: str
    family: str
    families: List[str]
    parameter_size: str
    quantization_level: str

    def __init__(self, **kwargs):
        names = set([f.name for f in fields(self)])
        for k, v in kwargs.items():
            if k in names:
                setattr(self, k, v)

@dataclass(init=False)
class Model:
    name: str
    model: str
    modified_at: datetime
    size: int
    digest: str
    details: Details

    def __init__(self, **kwargs):
        names = set([f.name for f in fields(self)])
        for k, v in kwargs.items():
            if k in names:
                if k == 'details':
                    v = Details(**v)
                setattr(self, k, v)

@dataclass(init=False)
class Tags:
    models: List[Model]

    def __init__(self, **kwargs):
        names = set([f.name for f in fields(self)])
        for k, v in kwargs.items():
            if k in names:
                if k == 'models':
                    v = [Model(**m) for m in v]
                setattr(self, k, v)


if __name__ == '__main__':
    tags_json = '''
    {
    "a":1,
        "models": [
            {
                "name": "qwen2.5:0.5b",
                "model": "qwen2.5:0.5b",
                "modified_at": "2024-12-31T18:07:19.156448474Z",
                "size": 397821319,
                "digest": "a8b0c51577010a279d933d14c2a8ab4b268079d44c5c8830c0a93900f1827c67",
                "details": {
                    "parent_model": "",
                    "format": "gguf",
                    "family": "qwen2",
                    "families": [
                        "qwen2"
                    ],
                    "parameter_size": "494.03M",
                    "quantization_level": "Q4_K_M"
                }
            }
        ]
    }
    '''
    import json

    print(Tags(**json.loads(tags_json)))